package com.wms.handler;

import com.basis.handler.service.DataColumn;
import com.basis.handler.service.DataImportService;
import com.basis.utils.CodeGenerateRuleUtil;
import com.mes.data.EmesVendor;
import com.wms.entity.WmsInOrder;
import org.apache.poi.ss.usermodel.Row;
import org.openbravo.dal.service.OBDal;
import org.openbravo.model.common.plm.Product;
import org.openbravo.model.common.uom.UOM;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

@SuppressWarnings("unchecked")
public class StorageDetailsImporter extends DataImportService {
	/**
	 * 开始存储记录
	 */
	protected void saveData(Row row, int rowIndex) throws Exception {
		Object colValue = null;
		Object value = null;
		DataColumn column = null;
		List values = new ArrayList();
		
//		String idName = tableName.toUpperCase() + "_ID";
//		boolean isProduct = "M_PRODUCT_ID".equals(idName);
//		String idValue = CodeNumberFactory.getUUID();
		
		int columnSize = row.getLastCellNum();
		Map valueMap = new HashMap();
		for (int i = 0; i < columnSize; i++) {
			colValue = readValue(row.getCell(i));
			valueMap.put(titleMap.get(i), colValue);
		}
		Map valMap = new HashMap();
		for (int i = 0; i < columnSize; i++) {
			column = columnMap.get(titleMap.get(i));
			if (column == null) {
				continue;
			}
			colValue = readValue(row.getCell(i));
			if (colValue != null && !"".equals(colValue)) {
				value = getDataValue(colValue, column, valueMap);
				if (value != null) {
					//唯一性验证
					if (column.isUnique()) {
						Long size = null;
						try {
							String checkSql = "select count(*) from " + tableName + " where " + column.getName()
									+ " = ?";
							size = (Long) getSqlValue(checkSql, value);
						} catch (Exception e) {
							throw new RuntimeException("数据唯一性验证时异常！[" + value + "]<br/>" + e.getMessage());
						}
						if (size > 0) {
							if (column.isIgnore()) {
								return;
							}
							throw new RuntimeException("第[ " + rowIndex + " ]行 “" + titleMap.get(i)
									+ "” 在记录已存在，此值必须唯一！");
						}
					}
					values.add(value);
//					if (isProduct && "VALUE".equals(column.getName().toUpperCase())) {
//						idValue = (String) value;
//					}
					valMap.put(column.getName().toUpperCase(), value);
				} else {
					throw new RuntimeException("第[ " + rowIndex + " ]行 “" + titleMap.get(i) + "” 值 “" + colValue
							+ "” 在选项中未找到！");
				}
			} else if (column.isNotNull()) {
				throw new RuntimeException("第[ " + rowIndex + " ]行 “" + titleMap.get(i) + "” 不能为空！");
			}
		}
		savePurchaseOrder(valMap);
		OBDal.getInstance().flush();
	}
	
	public void savePurchaseOrder(Map valMap) throws ParseException {
		WmsInOrder wio = new WmsInOrder();
		//单位
		wio.setUom(OBDal.getInstance().get(UOM.class, valMap.get("C_UOM_ID")));
		//产品
		Product product = (Product) OBDal.getInstance().get(Product.class, valMap.get("M_PRODUCT_ID"));
		wio.setProduct(product);
		//编码
		String code = CodeGenerateRuleUtil.getCode("B7FC5C2BD8424D489F7C464849A0855F", "Batch_No");
		wio.setBatchNo(code);
		//供应商
		EmesVendor ev = OBDal.getInstance().get(EmesVendor.class, valMap.get("Emes_VENDOR_ID"));
		wio.setEmesVendor(ev);
		//数量
		wio.setQty((BigDecimal) valMap.get("QTY"));
		//日期
		wio.setOrderDate(new Date());
		OBDal.getInstance().save(wio);
	}
}
